package com.primb.baselibs.override;

import android.graphics.Rect;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by Chen on 2017/3/20.
 * 功能描述：
 */

public class SpaceItemDecoration extends RecyclerView.ItemDecoration {

    private int space;

    public static final int ALL_AROUND = 101;

    private int orientation = OrientationHelper.VERTICAL;

    public SpaceItemDecoration(int orientation, int space) {
        this.orientation = orientation;
        this.space = space;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        if (orientation == OrientationHelper.HORIZONTAL) {
            if (parent.getChildAdapterPosition(view) == 0) {
                outRect.left = space;
                outRect.right = space;
            } else if (parent.getChildAdapterPosition(view) == parent.getChildCount() - 1) {
                outRect.left = space;
                outRect.right = space;
            } else {
                outRect.left = space;
                outRect.right = space;
            }
        } else if (orientation == ALL_AROUND) {
            if (parent.getChildAdapterPosition(view) == 0) {
                outRect.top = space;
                outRect.bottom = space / 2;
                outRect.left = space;
                outRect.right = space;
            } else if (parent.getChildAdapterPosition(view) == parent.getChildCount() - 1) {
                outRect.bottom = space;
                outRect.top = space / 2;
                outRect.right = space;
                outRect.left = space;
            } else {
                outRect.top = space / 2;
                outRect.bottom = space / 2;
                outRect.left = space;
                outRect.right = space;
            }
        } else {
            if (parent.getChildAdapterPosition(view) == 0) {
                outRect.top = space;
                outRect.bottom = space / 2;
            } else if (parent.getChildAdapterPosition(view) == parent.getChildCount() - 1) {
                outRect.bottom = space;
                outRect.top = space / 2;
            } else {
                outRect.top = space / 2;
                outRect.bottom = space / 2;
            }
        }
    }
}
